package com.qiguliuxing.dts.common.util;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class SystemInfoPrinter {
	public static final String CREATE_PART_COPPER = "DTS-SHOP";

	private static int maxSize = 0;
	private static int labelMaxLength = 0;

	public static void printInfo(String title, Map<String, String> infos) {
		setMaxSize(infos);

		printHeader(title);

		for (Map.Entry<String, String> entry : infos.entrySet()) {
			printLine(entry.getKey(), entry.getValue());
		}

		printEnd();
	}

	private static void setMaxSize(Map<String, String> infos) {
		for (Map.Entry<String, String> entry : infos.entrySet()) {
			if (entry.getValue() == null) {
                continue;
            }
			int size = entry.getKey().length() + entry.getValue().length();
			int labelSize = entry.getKey().getBytes().length;
			if (size > maxSize) {
                maxSize = size;
            }
			if (labelSize > labelMaxLength) {
				labelMaxLength = labelSize;
			}
		}
		maxSize = maxSize + 30;
		labelMaxLength = labelMaxLength + 2;
	}

	private static void printHeader(String title) {
		System.out.println(getLineCopper());
		System.out.println("");
		System.out.println("    " + title);
	}

	private static void printEnd() {
		System.out.println("  ");
		System.out.println(getLineCopper());
	}

	private static String getLineCopper() {
		String copper = "";
		for (int i = 0; i < maxSize; i++) {
			copper += "=";
		}
		return copper;
	}

	private static void printLine(String head, String line) {
		if (line == null) {
            return;
        }

		if (head.startsWith(CREATE_PART_COPPER)) {
			System.out.println("");
			System.out.println("    [[  " + line + "  ]]");
		} else {
			System.out.println("    " + formatLength(head) + "->        " + line);
			//System.out.println("    " + String.format(Locale.CHINA,"%1$-15s", head) + "->        " + line);
		}
	}

	private static String formatLength(String head) {
		String content = head;
		try {
			byte[] bs = head.getBytes("GBK");
			int needBank = labelMaxLength - (bs.length > 10 ? bs.length : bs.length + 1);
			String bankStr = "";
			for(int i = 0 ; i < needBank; i++){
				bankStr += " ";
			}
			content += bankStr;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return content;
	}
}
